package com.liang.circle.server.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.liang.circle.api.req.ShareCircleSaveReq;
import com.liang.circle.api.req.ShareCircleUpdateReq;
import com.liang.circle.api.vo.ShareCircleVO;
import com.liang.circle.server.entity.po.ShareCircle;
import com.liang.circle.server.mapper.ShareCircleMapper;
import com.liang.circle.server.service.ShareCircleService;
import com.liang.circle.server.util.LoginUtil;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ShareCircleServiceImpl extends ServiceImpl<ShareCircleMapper, ShareCircle> implements ShareCircleService {

    private static final Cache<Integer, List<ShareCircleVO>> CACHE = Caffeine.newBuilder()
            .initialCapacity(1)
            .maximumSize(1)
            .expireAfterWrite(Duration.ofSeconds(30))
            .build();

    @Override
    public void saveCircle(ShareCircleSaveReq requestParam) {
        ShareCircle shareCircle = BeanUtil.copyProperties(requestParam, ShareCircle.class);
        save(shareCircle);
        CACHE.invalidateAll();
    }

    @Override
    public void updateCircle(ShareCircleUpdateReq requestParam) {
        lambdaUpdate()
                .eq(requestParam.getId() != null, ShareCircle::getId, requestParam.getId())
                .update(BeanUtil.copyProperties(requestParam, ShareCircle.class));
        CACHE.invalidateAll();
    }

    @Override
    public void removeCircle(Long id) {
        removeById(id);
        CACHE.invalidateAll();
    }

    @Override
    public List<ShareCircleVO> listCircle() {
        List<ShareCircleVO> res = CACHE.getIfPresent(1);
        if (res != null) {
            return res;
        }
        List<ShareCircle> list = list();
        // 获取父级圈子
        List<ShareCircle> parentList = list.stream()
                .filter(item -> item.getParentId() == -1)
                .collect(Collectors.toList());
        // 根据父圈子分组
        Map<Long, List<ShareCircle>> map = list.stream().collect(Collectors.groupingBy(ShareCircle::getParentId));
        List<ShareCircleVO> voList = parentList.stream().map(item -> {
            // 拷贝VO
            ShareCircleVO shareCircleVO = BeanUtil.copyProperties(item, ShareCircleVO.class);
            // 获取当前圈子的子圈子
            List<ShareCircle> subCircles = map.get(item.getId());
            if (CollUtil.isEmpty(subCircles)) {
                shareCircleVO.setChildren(Collections.emptyList());
            } else {
                List<ShareCircleVO> subCircleVOList = subCircles.stream().map(subItem -> {
                    return BeanUtil.copyProperties(subItem, ShareCircleVO.class);
                }).collect(Collectors.toList());
                shareCircleVO.setChildren(subCircleVOList);
            }
            return shareCircleVO;
        }).collect(Collectors.toList());
        CACHE.put(1, voList);
        return voList;
    }
}
